package user.main;

import filewriting.FitnessViewWriter;
import guiobjects.Observation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import user.data.Data;
import commonfunctions.Functions;
import commonfunctions.Matrix;
import commonfunctions.GetStats;
import datacreation.*;


/**
 * This class is for debugging. Runs most of the programs in the system, creating a new group of
 * observations, tracking them, then displaying the results.
 * 
 * @author Trevor Terris
 *
 */
public class RunStatistics 
{
	public static void main (String[] args)
	{
		try 
		{
			int numFalsePos = 0;
			int numFalseNeg = 0;
			double totalScore = 0.0;
			System.out.println("Finds the number of false positives, false negatives, and total score over " + Data.NUM_TRIALS + " trials.");
			System.out.println();
	
	    	System.out.println("What directory should be created for data?");
	    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    	String dirName = br.readLine();
	    	
	    	for (int k = 0; k < Data.NUM_TRIALS; k++)
	    	{
				//creates the group of observations
		    	ArrayList<Observation> obs;
		    	obs = ObservationCreator.createData(dirName);
	
		    	Matrix weights;
		    	weights = WeightsCreator.createData(obs, dirName);
	
				Matrix tracks = TracksCreator.createData(obs, weights, dirName);
				
				totalScore += Functions.getPathSums(weights, tracks);
				
				int[] stats = GetStats.print(obs, weights, tracks);
				numFalsePos += stats[0];
				numFalseNeg += stats[1];
				
				ArrayList<Boolean> hasWritten = new ArrayList<Boolean>();
				ArrayList<Thread> writingThreads = new ArrayList<Thread>();
				
		    	writingThreads.add(FitnessViewWriter.startWriting(obs, weights, tracks, dirName));
		    	hasWritten.add(false);
		    	
		    	
		    	int numWritten = 0;
	
		    	do
		    	{
		    		for (int i = 0; i < writingThreads.size(); i++)
		    		{
		    			if (!hasWritten.get(i) && !writingThreads.get(i).isAlive())
		    			{
		    				hasWritten.set(i, true);
		    				numWritten++;
		    			}
		    			if (numWritten < writingThreads.size())
		    				Thread.sleep(100);
		    		}
		    	} while (numWritten < writingThreads.size());
				
				System.out.println("Done with iteration " + (k + 1) + ".");
	    	}
	    	System.out.println("Total false positives: " + numFalsePos);
	    	System.out.println("Total false negatives: " + numFalseNeg);
	    	System.out.println("Total sum of scores: " + totalScore);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("IO exception.");
		}
	}
}
